/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   SkipList_test.cpp                                  :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: TYTY000 <767280080@qq.com>                 +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/04/23 22:00:20 by TYTY000           #+#    #+#             */
/*   Updated: 2024/04/24 10:17:26 by TYTY000          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <iostream>
#include <memory>
using namespace std;
#include "../print/skiplist.h"
#include "../vector/vector.h"
#include "./QuadList.h"
#include "SkipList.h"

#define dice(X) (rand() % (X))

template <typename K, typename V> // key¡¢value
void testSkiplist(Rank n) {
  auto L = make_shared<Skiplist<K, V>>();
  // auto keys = Vector<K>();
  while (L->size() < n)
    switch (dice(3)) {
    case 0: {                 // ²éÕÒ£¬³É¹¦ÂÊ <= 33.3%
      K key = dice((K)n * 3); //[0, 3n)·¶Î§ÄÚµÄkey
      printf("Searching for ");
      print(key);
      printf(" ... ");
      V *pValue = L->get(key);
      if (pValue) {
      } else
        printf("Not found");
      printf("\n\n");
      break;
    }
    case 1: {                 // É¾³ý£¬³É¹¦ÂÊ <= 33.3%
      K key = dice((K)n * 3); //[0, 3n)·¶Î§ÄÚµÄkey
      printf("Removing ");
      print(key);
      printf(" ... ");
      if (L->remove(key)) {
        // keys.rm(key);
        printf("Done\n");
        print(*L.get());
      } else
        printf("Entry not exists\n\n");
      break;
    }
    default: { // ²åÈë£¬³É¹¦ÂÊ == 100%
      K k = dice((K)n * 3);
      V v = (V)'A' + dice(26); // ÔÚ[0, 2n)*['A'~'Z']·¶Î§ÄÚµÄ´ÊÌõ
      printf("Inserting <");
      print(k);
      printf(",");
      print(v);
      printf("> ... ");
      L->put(k, v);
      // keys.insert(k);
      printf("Done\n");
      // std::cout << keys;
      print(*L.get());
      break;
    }
    }

  // for (auto key : keys) {
  //   printf("Removing ");
  //   print(key);
  //   printf(" ... ");
  //   L->remove(key) ? printf("Done\n") : printf("Entry not exists\n");
  // }
  // print(*L.get());
  // printf("\n");
}

/******************************************************************************************
 * Ö÷³ÌÐò
 ******************************************************************************************/
int main(int argc, char *argv[]) {
  if (2 > argc) {
    printf("Usage: %s <size of test>\a\a\n", argv[0]);
    return 1;
  }
  srand((unsigned int)time(NULL)); // Ëæ»úÖÖ×Ó
  // srand( 31415926 ); //¹Ì¶¨ÖÖ×Ó£¨¼ÙÖÖ×Ó£¬µ÷ÊÔÓÃ£©
  testSkiplist<int, char>(atoi(argv[1])); // ÔªËØÀàÐÍ¿ÉÒÔÔÚÕâÀïÈÎÒâÑ¡Ôñ
  return 0;
}
